/*监听输入框值变化*/
$("#uName,#uPwd,#reuPwd").focus(function(){
    $("#uName,#uPwd,#reuPwd").on("input propertychange", function () {
        var $this=$(this);
        var myStr=$this.val();
        if(myStr.length > 0){
            $this.next().show();
        }else{
            $this.next().hide();
        }
    });
});
$("#uName,#uPwd,#reuPwd").blur(function(){
    var $this=$(this);
    $this.next().hide();
});
/*清空表单*/
$(".clear").click(function () {
    var $this = $(this);
    $this.prev().val("");
    $this.hide();
});

$("#registBtn").click(checkuName);

var uName = "";
var uPwd = "";
var reg = /^[\u4e00-\u9fa5A-Za-z0-9_]{3,16}$/;//用户名正则表达式
var pattern = /^[A-Za-z0-9_]{6,14}$/;//密码正则表达式
/*验证用户名*/
function checkuName(){
    uName=$.trim($('#uName').val());
    if (uName == '') {
        $('#msg').text('用户名不能为空！').fadeIn();
        return;
    } else {
        if (!reg.test(uName)) {
            $('#msg').text('用户名格式不正确！').fadeIn();
            return;
        } else {
            $('#msg').text('').fadeOut();
            checkuPwd();
        }
    }
};
/*验证密码*/
function checkuPwd(){
    uPwd=$.trim($('#uPwd').val());
    if (uPwd == '') {
        $('#msg').text('密码不能为空！').fadeIn();
        return;
    } else {
        if (!pattern.test(uPwd)) {
            $('#msg').text('密码格式不正确！').fadeIn();
            return;
        } else {
            $('#msg').text('').fadeOut();
            checkReuPwd();
        }
    }
};
/*验证确认密码*/
function checkReuPwd() {
    var reuPwd=$.trim($('#reuPwd').val());
    if (reuPwd == '') {
        $('#msg').text('请确认密码！').fadeIn();
        return;
    } else {
        if (reuPwd !== uPwd) {
            $('#msg').text('两次密码不一致！').fadeIn();
            return;
        } else {
            $('#msg').text('').fadeOut();
            validateRepeat();
        }
    }
};
function validateRepeat(){
	$.ajax({
	    type: "post",
	    url: "validateRepeatAction.do",
	    data: {
	        userName: uName,
	    },
	    dataType: "json",
	    success: function (data) {
	        if (data.status == "succ") {
	        	validate();
	        }else if(data.status == "fail"){
	        	$('#msg').text('用户名已存在！').fadeIn();
	        	$("#uName,#uPwd,#reuPwd").val('');
	        	return;
	        }
	    }
	});	
}

/*注册并返回结果*/
function validate() {
    $.ajax({
        type: "post",
        url: "studentRegisterAction.do",
        data: {
            userName: uName,
            passWord: uPwd
        },
        dataType: "json",
        success: function (data) {
            if (data.status == "succ") {
            	alert('注册成功！');
                window.location.href = '../../login.html';
            }
        }
    });
}

